/*
 *  Copyright (c) 2010 Brian Stanek <bstanek@gmail.com>
 *
 *  Permission is hereby granted, free of charge, to any person
 *  obtaining a copy of this software and associated documentation
 *  files (the "Software"), to deal in the Software without
 *  restriction, including without limitation the rights to use,
 *  copy, modify, merge, publish, distribute, sublicense, and/or sell
 *  copies of the Software, and to permit persons to whom the
 *  Software is furnished to do so, subject to the following
 *  conditions:
 *
 *  The above copyright notice and this permission notice shall be
 *  included in all copies or substantial portions of the Software.
 *
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 *  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 *  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 *  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 *  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 *  OTHER DEALINGS IN THE SOFTWARE.
 */

package guice
{

import flash.utils.getDefinitionByName;
import flash.utils.getQualifiedClassName;
import mx.utils.DescribeTypeCache;

/**
 *  Binding key consisting of an injection type and an optional annotation.
 *  Matches the type and annotation at a point of injection.
 *
 *  <p>For example, <code>new Key(Service, "Transactional")</code> will match:
 *
 *  <pre>
 *      [Inject(service="Transactional")]
 *      public function setService(service:Service):void
 *      {
 *          ...
 *      }
 *  </pre>
 *
 */
public class Key
{

    //--------------------------------------------------------------------------
    //
    //  Constructor
    //
    //--------------------------------------------------------------------------

    /**
     *  Constructs a new Key with the specified <code>type</code> and
     *  <code>annotation</code>.
     *
     *  @param type <code>Class</code> or <code>String</code> indicating the
     *  <code>type</code> for the new <code>Key</code>.
     *
     *  @param annotation
     *
     */
    public function Key(type:*, annotation:String="")
    {
        super();
        if (type is Class)
        {
            initWithType(Class(type));
        }
        else if (type is String)
        {
            initWithString(String(type));
        }
        else
        {
            throw new Error("Invalid type for Key type");
        }
        this.annotation = annotation;
        computeHashCode();
    }

    /**
     *  @private
     */
    private function initWithType(type:Class):void
    {
        this.type = type;
        fullyQualifiedTypeName = DescribeTypeCache.describeType(type).typeName;
    }

    /**
     *  @private
     */
    private function initWithString(typeName:String):void
    {
        fullyQualifiedTypeName = DescribeTypeCache.describeType(typeName).typeName;
        type = Class(getDefinitionByName(fullyQualifiedTypeName));
    }

    /**
     *  @private
     */
    private function computeHashCode():void
    {
        hashCode = fullyQualifiedTypeName;
        if (annotation)
        {
            hashCode += "|" + annotation;
        }
    }

    //--------------------------------------------------------------------------
    //
    //  Variables
    //
    //--------------------------------------------------------------------------

    /**
     *  @private
     *  The String <code>annotation</code> for this <code>Key</code>.
     */
    private var annotation:String;

    /**
     *  @private
     *  The internal Flash player name for this <code>Key</code>'s class.
     *
     *  <p>
     *  For example, <code>"guice::Key"</code>.
     *  </p>
     */
    private var fullyQualifiedTypeName:String;

    /**
     *  @private
     *  A unique hash for this <code>Key</code>'s type/annotation.
     */
    private var hashCode:String;

    /**
     *  @private
     *  The Class <code>type</code> for this <code>Key</code>.
     */
    private var type:Class;

    //--------------------------------------------------------------------------
    //
    //  Methods
    //
    //--------------------------------------------------------------------------

    /**
     *  @return The String <code>annotation</code> for this <code>Key</code>.
     */
    public function getAnnotation():String
    {
        return annotation;
    }

    /**
     *  @return A unique hash for this <code>Key</code>'s type/annotation.
     */
    public function getHashCode():String
    {
        return hashCode;
    }

    /**
     *  @return The Class <code>type</code> for this <code>Key</code>.
     */
    public function getType():Class
    {
        return type;
    }

    /**
     *  Creates a String representation of this <code>Key</code>.
     *
     *  @return String representation of this <code>Key</code>.
     */
    public function toString():String
    {
        return "Key[type=" + fullyQualifiedTypeName + ", annotation=" +
                annotation + "]";
    }

    /**
     *  @return <code>true</code> if the passed-in <code>Key</code> is equal
     *  to this <code>Key</code>.
     */
    public function equals(other:Object):Boolean
    {
        if (this == other)
        {
            return true;
        }
        if (!(other is Key))
        {
            return false;
        }
        return hashCode == other.getHashCode();
    }

}

}
